
//
//  NewAddProductViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/5/14.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kItemWidth (App_Frame_Width - (15*5))/4

#import "NewAddProductViewController.h"

#import "BHSingleButton.h"
#import "UIButton+WebCache.h"
#import "TZImagePickerController.h"
#import "UIViewController+BHMediaExt.h"

@interface NewAddProductViewController ()<UITextFieldDelegate, UITextViewDelegate,TZImagePickerControllerDelegate>

@property (nonatomic, strong) UITextField *productTF;
@property (nonatomic, strong) UITextView *infoTV;
@property (nonatomic, strong) UIButton *showImageBtn;
@property (nonatomic, strong) UIImage *showImage;
@property (nonatomic, strong) BHSingleButton *finishBtn;

@end

@implementation NewAddProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"新增产品";
    self.view.backgroundColor = Bg_Color_FFFFFF;
    [self initNavBarBackBtn];
    [self setUpUI];
    
    if (![BHSettingUtil dataAndStringIsNull:self.productModel]) {
        [self loadModelInfo];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpUI {
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"产品名称：";
    nameLabel.textColor = Text_Color_333333;
    nameLabel.font = BHFont_Size_15;
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(30);
        make.top.equalTo(self.view.mas_top).offset(25);
        make.width.offset([BHSettingUtil widthForText:@"产品名称：" fontSize:15]-10);
    }];
    
    self.productTF = [[UITextField alloc] init];
    self.productTF.delegate = self;
    self.productTF.placeholder = @"请输入产品名称";
    [self.view addSubview:self.productTF];
    [self.productTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(-10);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.offset(45);
        make.centerY.equalTo(nameLabel);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = Bottom_Line_Color;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.height.offset(0.5);
        make.centerX.equalTo(self.view);
        make.top.equalTo(nameLabel.mas_bottom).offset(10);
    }];
    
    UILabel *infoLabel = [[UILabel alloc] init];
    infoLabel.text = @"简介：";
    infoLabel.textColor = Text_Color_333333;
    infoLabel.font = BHFont_Size_15;
    [self.view addSubview:infoLabel];
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_left);
        make.top.equalTo(line.mas_bottom).offset(10);
        make.width.offset([BHSettingUtil widthForText:@"简介：" fontSize:15]-10);
    }];
    
    self.infoTV = [[UITextView alloc] init];
    self.infoTV.delegate = self;
    [self.view addSubview:self.infoTV];
    [self.infoTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(infoLabel.mas_left);
        make.centerX.equalTo(self.view);
        make.top.equalTo(infoLabel.mas_bottom).offset(10);
        make.height.offset(130);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = Bg_Color_EEEEEE;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(15);
        make.top.equalTo(self.infoTV.mas_bottom).offset(10);
    }];
    
    UILabel *picLabel = [[UILabel alloc] init];
    picLabel.text = @"添加图片：";
    picLabel.textColor = Text_Color_333333;
    picLabel.font = BHFont_Size_15;
    [self.view addSubview:picLabel];
    [picLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_left);
        make.top.equalTo(bgView.mas_bottom).offset(18);
        make.width.offset([BHSettingUtil widthForText:@"添加图片：" fontSize:15]-10);
    }];
    
    self.showImageBtn = [[UIButton alloc] init];
    [self.showImageBtn setImage:[UIImage imageNamed:@"my-4"] forState:UIControlStateNormal];
    //    [self.showImageBtn setImage:self.showImage forState:UIControlStateNormal];
    [self.showImageBtn addTarget:self action:@selector(_selectPictureCamera) forControlEvents:UIControlEventTouchUpInside];
    self.showImageBtn.layer.masksToBounds = YES;
    self.showImageBtn.layer.cornerRadius = 3;
    [self.view addSubview:self.showImageBtn];
    [self.showImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(picLabel);
        make.height.width.offset(60);
//        make.centerX.equalTo(self.view);
        make.top.equalTo(picLabel.mas_bottom).offset(10);
    }];
    
    _finishBtn = [[BHSingleButton alloc] initWithFrame:CGRectMake(10, App_Frame_Height-kNavbarAndStatusBar-120, App_Frame_Width-(10*2), 60)];
    _finishBtn.button_name(@"确定");
    [_finishBtn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_finishBtn];
}

- (void)loadModelInfo {
    self.productTF.text = [BHSettingUtil dataAndStringIsNull:self.productModel.product_name]?@"":self.productModel.product_name;
    self.infoTV.text = [BHSettingUtil dataAndStringIsNull:self.productModel.Description]?@"":self.productModel.Description;
    if (![BHSettingUtil dataAndStringIsNull:self.productModel.pic]) {
        NSString *picStr = [NSString stringWithFormat:@"%@%@", BH_PicPreString, self.productModel.pic];
        [self.showImageBtn sd_setImageWithURL:[NSURL URLWithString:picStr] forState:UIControlStateNormal];
//        [self.showImageBtn setImage:[UIImage ] forState:UIControlStateNormal];
    }
}

#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    self.showImage = photos[0];
    [self.showImageBtn setImage:[BHSettingUtil dataAndStringIsNull:self.showImage]?[UIImage imageNamed:@"my-4"]:self.showImage forState:UIControlStateNormal];
}


#pragma mark -
- (void)_selectPictureCamera {
    [UIViewController showAlertWithTitle:nil message:nil actionTitles:@[@"相册"] cancelTitle:@"取消" style:UIAlertControllerStyleActionSheet completion:^(NSInteger index) {
        if (index == 0) {
            TZImagePickerController *imagePickController = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
            imagePickController.allowPickingVideo = NO;
            [self presentViewController:imagePickController animated:YES completion:nil];
        }
    }];
}

- (void)finishAction {
    if([BHSettingUtil dataAndStringIsNull:self.productTF.text]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请输入产品名称"];
    }
    else if([BHSettingUtil dataAndStringIsNull:self.infoTV.text]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请输入产品简介"];
    }
    else if([BHSettingUtil dataAndStringIsNull:self.showImage]) {
        if ([BHSettingUtil dataAndStringIsNull:self.productModel]) {
            [[BHAlertUtil alertManager] showPromptInfo:@"请上传产品图片"];
        } else {
            [self requestPostBH_User_editProduct];
        }
    }
    else {
        if ([BHSettingUtil dataAndStringIsNull:self.productModel]) {
            //新增
            [self requestPostBH_User_addProduct];
        } else {
            //编辑
            [self requestPostBH_User_editProduct];
        }
    }
}

- (void)requestPostBH_User_addProduct {
    SGUserModel *userModel = [SGUserInfo getUserInfo];
    
    [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    [params setObject:@"14" forKey:@"company_id"];
    if (![BHSettingUtil dataAndStringIsNull:userModel.company_id]) {
        [params setObject:userModel.company_id forKey:@"company_id"];
    }
    else {
        
    }
    [params setObject:self.productTF.text forKey:@"product_name"];
    //    [params setObject:self.imageArray[0] forKey:@"pic"];
    [params setObject:self.infoTV.text forKey:@"description"];
    
    [PPHTTPRequest requestPostBH_User_addProduct:^(id response, BOOL success) {
        if (success) {
            [[BHAlertUtil alertManager] showPromptInfo:@"新增成功"];
            [self.navigationController popViewControllerAnimated:YES];
            if (self.ifRefreshBlock) {
                self.ifRefreshBlock(YES);
            }
        }
        else {
            //            @WeakObj(self);
            //            [self.cycleView.placeholderShow(YES) actionWithBlock:^{
            //                [selfWeak requestPostBH_Exhibition_getPicByBoothId];
            //            }];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params images:self.showImage];
}

//编辑
- (void)requestPostBH_User_editProduct {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [[BHAlertUtil alertManager]showLoading:nil];
    [params setObject:self.productModel.Id forKey:@"id"];
    if (![self.productTF.text isEqualToString:self.productModel.product_name]) {
        [params setObject:self.productTF.text forKey:@"product_name"];
    }
    if (![self.infoTV.text isEqualToString:self.productModel.Description]) {
        [params setObject:self.infoTV.text forKey:@"description"];
    }
//    [params setObject:self.showImage forKey:@"pic"];
    if (![BHSettingUtil dataAndStringIsNull:self.showImage]) {
        [PPHTTPRequest requestPostBH_User_editProduct:^(id response, BOOL success) {
            [[BHAlertUtil alertManager]hiddenLoading];
            if (success) {
                [[BHAlertUtil alertManager] showPromptInfo:@"编辑成功"];
                [self.navigationController popViewControllerAnimated:YES];
                if (self.ifRefreshBlock) {
                    self.ifRefreshBlock(YES);
                }
            }
            else
            {
                
            }
        } params:params images:self.showImage];
    } else {
        [PPHTTPRequest requestPostBH_User_editProduct:^(id response, BOOL success) {
            [[BHAlertUtil alertManager]hiddenLoading];
            if (success) {
                [[BHAlertUtil alertManager] showPromptInfo:@"编辑成功"];
                [self.navigationController popViewControllerAnimated:YES];
                if (self.ifRefreshBlock) {
                    self.ifRefreshBlock(YES);
                }
            }
            else
            {
                
            }
        } params:params];
    }
}


@end
